<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  
  <title>WEI BLOG</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  
  
  
    <link rel="alternate" href="/wei-blog/atom.xml" title="WEI BLOG" type="application/atom+xml">
  
  
    <link rel="shortcut icon" href="/wei-blog/favicon.png">
  
  
    
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/typeface-source-code-pro@0.0.71/index.min.css">

  
  
<link rel="stylesheet" href="/wei-blog/css/style.css">

  
  
  
  
<meta name="generator" content="Hexo 6.3.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a id="logo">WEI BLOG</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        
          <a class="main-nav-link" href="/wei-blog/."><i class="fa fa-home"></i> 首页</a>
        
          <a class="main-nav-link" href="/wei-blog/archives/"><i class="fa fa-archive"></i> 归档</a>
        
      </nav>
    </div>
    <div id="search-form">
      <div id="result-mask" class="hide"></div>
      <label><input id="search-key" type="text" autocomplete="off" placeholder="搜索"></label>
      <div id="result-wrap" class="hide">
        <div id="search-result"></div>
      </div>
      <div class="hide">
        <template id="search-tpl">
          <div class="item">
            <a href="/{path}" title="{title}">
              <div class="title">{title}</div>
              <!-- <div class="time">{date}</div> -->
              <div class="tags">{tags}</div>
            </a>
          </div>
        </template>
      </div>
    </div>
  </div>
</header>

      <div class="outer">
        <section id="main">
  <article id="post-使用-redis-bitmap-实现签到功能" class="h-entry article article-type-post" itemprop="blogPost" itemscope itemtype="https://schema.org/BlogPosting">
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="p-name article-title" href="/wei-blog/source/%E4%BD%BF%E7%94%A8-redis-bitmap-%E5%AE%9E%E7%8E%B0%E7%AD%BE%E5%88%B0%E5%8A%9F%E8%83%BD/">使用 redis bitmap 实现签到功能</a>
    </h1>
  


      </header>
    
    <div class="article-meta">
      
      <!-- <span class="article-date">
  <i class="fa fa-date"></i>
  <time class="dt-published" datetime="2023-02-05T07:26:59.000Z" itemprop="datePublished">2023年02月05日</time>
</span> -->
      
  <div class="article-category">
    <i class="fa fa-classify"></i>
    <a class="article-category-link" href="/wei-blog/categories/Redis/">Redis</a>
  </div>

      
      
<!-- <a href="/wei-blog/source/%E4%BD%BF%E7%94%A8-redis-bitmap-%E5%AE%9E%E7%8E%B0%E7%AD%BE%E5%88%B0%E5%8A%9F%E8%83%BD/#comments" class="article-comment-link">
  
  <i class="fa fa-commt"></i>
  留言
</a> -->


    </div>
    <div class="e-content article-entry" itemprop="articleBody">
      
        <h1 id="核心逻辑"><a href="#核心逻辑" class="headerlink" title="核心逻辑"></a>核心逻辑</h1><p>用户签到信息按月存储：<code>user:sign:论坛ID:用户ID:月份</code> </p>
<p>判断用户是否签到：<code>GETBIT user:sign:论坛ID:用户ID:月份 0</code> 获取用户某月1日签到情况</p>
<p>用户签到：<code>SETBIT user:sign:论坛ID:用户ID:月份 0 1</code> 用户在某月1日进行签到</p>
<p>获取用户某月签到数据：<code>BITFIELD user:sign:论坛ID:用户ID:月份 GET u31 0</code> 获取用户某月的签到情况</p>
<p>​	<code>u31</code> u：无符号数 31：获取的数据长度</p>
<p>返回用户总签到次数：<code>BITCOUNT user:sign:论坛ID:用户ID:月份 0 3</code></p>
<p>​	<code>0 3</code> BITCOUNT获取1的个数时下标并不是按位计算的，而是按字节（八位）计算 </p>
<p>​	<code>0 0</code> 获取第一个字节的中1的个数</p>
<p>​	<code>0 1</code> 获取第一个字节到第二个字节中1的个数</p>
<h1 id="获取连续签到天数"><a href="#获取连续签到天数" class="headerlink" title="获取连续签到天数"></a>获取连续签到天数</h1><h2 id="代码逻辑"><a href="#代码逻辑" class="headerlink" title="代码逻辑"></a>代码逻辑</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 获取连续签到天数</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> userId 用户id</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> forumId 论坛id</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@return</span> 连续签到天数</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">private</span> Integer <span class="title function_">getConsecutiveDays</span><span class="params">(<span class="type">int</span> userId, <span class="type">int</span> forumId)</span> &#123;</span><br><span class="line">    <span class="type">String</span> <span class="variable">userSignHashKey</span> <span class="operator">=</span> forumId + <span class="string">&quot;:&quot;</span> + userId;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">// 存在签到天数hash</span></span><br><span class="line">    <span class="keyword">if</span> (redisUtil.hasKey(RedisKey.USER_SIGN_DAY)) &#123;</span><br><span class="line">        <span class="comment">// 查询保存的连续签到天数</span></span><br><span class="line">        <span class="type">Integer</span> <span class="variable">day</span> <span class="operator">=</span> (Integer) redisUtil.getHash(RedisKey.USER_SIGN_DAY, userSignHashKey);</span><br><span class="line">        <span class="comment">// 有保存的签到天数</span></span><br><span class="line">        <span class="keyword">if</span> (day != <span class="literal">null</span>) &#123;</span><br><span class="line">            <span class="keyword">return</span> day;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="comment">// 保存签到天数hash并设置过期时间</span></span><br><span class="line">        <span class="type">String</span> <span class="variable">expireDate</span> <span class="operator">=</span> DateUtil.today() + <span class="string">&quot; 23:59:59&quot;</span>;</span><br><span class="line">        redisUtil.setHashWithDateExpire(RedisKey.USER_SIGN_DAY, <span class="string">&quot;23:59:59&quot;</span>, <span class="string">&quot;23:59:59&quot;</span>, expireDate);</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">// 没有保存的签到天数</span></span><br><span class="line">    <span class="type">LocalDate</span> <span class="variable">now</span> <span class="operator">=</span> LocalDate.now();</span><br><span class="line">    <span class="type">String</span> <span class="variable">userSignKey</span> <span class="operator">=</span> </span><br><span class="line">            generateUserSingKey(userId, forumId, now);</span><br><span class="line">    <span class="type">int</span> <span class="variable">dayOfMonth</span> <span class="operator">=</span> now.getDayOfMonth();</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 查询本月到当日为止的签到记录</span></span><br><span class="line">    <span class="type">Long</span> <span class="variable">totalBitmap</span> <span class="operator">=</span> redisUtil.getTotalBitmap(userSignKey, dayOfMonth);</span><br><span class="line">    <span class="comment">// 判断连续签到天数</span></span><br><span class="line">    <span class="type">int</span> <span class="variable">consecutiveDays</span> <span class="operator">=</span> <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">0</span>; i &lt; dayOfMonth; i++) &#123;</span><br><span class="line">        <span class="comment">// 判断当天是否签到</span></span><br><span class="line">        <span class="keyword">if</span> (totalBitmap &gt;&gt;&gt; <span class="number">1</span> &lt;&lt; <span class="number">1</span> != totalBitmap) &#123;</span><br><span class="line">            consecutiveDays++;</span><br><span class="line">            totalBitmap = totalBitmap &gt;&gt;&gt; <span class="number">1</span>;</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            redisUtil.setHash(RedisKey.USER_SIGN_DAY, userSignHashKey, consecutiveDays);</span><br><span class="line">            <span class="keyword">return</span> consecutiveDays;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 当月已全部签到，查询其它月份</span></span><br><span class="line">    <span class="type">LocalDate</span> <span class="variable">lastMonthDate</span> <span class="operator">=</span> <span class="literal">null</span>;</span><br><span class="line">    <span class="keyword">while</span> (<span class="literal">true</span>) &#123;</span><br><span class="line">        <span class="comment">// 获取上个月日期</span></span><br><span class="line">        <span class="keyword">if</span> (lastMonthDate == <span class="literal">null</span>) &#123;</span><br><span class="line">            lastMonthDate = now.minusMonths(<span class="number">1</span>);</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            lastMonthDate = lastMonthDate.minusMonths(<span class="number">1</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 上个月签到key</span></span><br><span class="line">        <span class="type">String</span> <span class="variable">lastMonthUserSignKey</span></span><br><span class="line">                <span class="operator">=</span> generateUserSingKey(userId, forumId, lastMonthDate);</span><br><span class="line">        <span class="comment">// 上个月总天数</span></span><br><span class="line">        <span class="type">int</span> <span class="variable">lastMonthTotalDays</span> <span class="operator">=</span> lastMonthDate.lengthOfMonth();</span><br><span class="line">        <span class="comment">// 查询上个月总签到天数</span></span><br><span class="line">        <span class="type">Long</span> <span class="variable">lastMonthSignDays</span> <span class="operator">=</span> redisUtil.getBitmapCount(lastMonthUserSignKey, <span class="number">0</span>, <span class="number">3</span>);</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (lastMonthSignDays == <span class="number">0</span>) &#123;</span><br><span class="line">            redisUtil.setHash(RedisKey.USER_SIGN_DAY, userSignHashKey, consecutiveDays);</span><br><span class="line">            <span class="keyword">return</span> consecutiveDays;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (lastMonthSignDays != lastMonthTotalDays) &#123;</span><br><span class="line">            <span class="type">Long</span> <span class="variable">lastMonthSignBitmap</span> <span class="operator">=</span> redisUtil.getTotalBitmap(lastMonthUserSignKey, lastMonthTotalDays);</span><br><span class="line">            <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">0</span>; i &lt; lastMonthTotalDays; i++) &#123;</span><br><span class="line">                <span class="comment">// 判断当天是否签到</span></span><br><span class="line">                <span class="keyword">if</span> (lastMonthSignBitmap &gt;&gt;&gt; <span class="number">1</span> &lt;&lt; <span class="number">1</span> != lastMonthSignBitmap) &#123;</span><br><span class="line">                    consecutiveDays++;</span><br><span class="line">                    lastMonthSignBitmap = lastMonthSignBitmap &gt;&gt;&gt; <span class="number">1</span>;</span><br><span class="line">                &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                    redisUtil.setHash(RedisKey.USER_SIGN_DAY, userSignHashKey, consecutiveDays);</span><br><span class="line">                    <span class="keyword">return</span> consecutiveDays;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (lastMonthSignDays == lastMonthTotalDays) &#123;</span><br><span class="line">            consecutiveDays = consecutiveDays + lastMonthTotalDays;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="RedisUtil"><a href="#RedisUtil" class="headerlink" title="RedisUtil"></a>RedisUtil</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 设置bitmap指定位置的值</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> key 要设置的键</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> offset 要设置的下标</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> value 要设置的值</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">setBitmap</span><span class="params">(String key, Long offset, Boolean value)</span> &#123;</span><br><span class="line">    redisTemplate.opsForValue().setBit(key, offset, value);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 获取bitmap指定位置上的值</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> key 要获取的键</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> offset 要获取的下标</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@return</span> Boolean key不存在或值为0 -&gt; false  值为1 -&gt; true</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> Boolean <span class="title function_">getBitmap</span><span class="params">(String key, Long offset)</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> redisTemplate.opsForValue().getBit(key, offset);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 获取bitmap的全部数据</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> key 要获取的键</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> length 要获取的长度</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@return</span> 该bitmap的所有位数  值不存在时返回0L</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> Long <span class="title function_">getTotalBitmap</span><span class="params">(String key, Integer length)</span> &#123;</span><br><span class="line">    <span class="type">BitFieldSubCommands</span> <span class="variable">bitFieldSubCommands</span> <span class="operator">=</span> BitFieldSubCommands.create()</span><br><span class="line">            .get(BitFieldSubCommands.BitFieldType.unsigned(length))</span><br><span class="line">            .valueAt(<span class="number">0</span>);</span><br><span class="line">    </span><br><span class="line">    List&lt;Long&gt; longList = redisTemplate.opsForValue().bitField(key, bitFieldSubCommands);</span><br><span class="line">    <span class="keyword">if</span> (longList == <span class="literal">null</span> || longList.size() != <span class="number">1</span>) &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="number">0L</span>;</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> longList.get(<span class="number">0</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 获取指定的字节范围中1的个数</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> key 要查询的key </span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> start 开始的字节下标</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> end 结束的字节下标</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@return</span> 1的个数</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> Long <span class="title function_">getBitmapCount</span><span class="params">(String key, Integer start, Integer end)</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> redisTemplate.execute((RedisCallback&lt;Long&gt;) con -&gt; con.bitCount(key.getBytes(), start, end));</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 保存值到hash</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> redisKey 要存入的redis键</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> hashKey 要存入的hash键</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> hashValue 要存入的hash值</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">setHash</span><span class="params">(String redisKey, String hashKey, Object hashValue)</span> &#123;</span><br><span class="line">    redisTemplate.opsForHash().put(redisKey, hashKey, hashValue);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 增加redis键中指定hash键的值</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> redisKey 要增加的redisKey</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> hashKey 要增加的hashKey</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> delta 增加量</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@return</span> 增加后的值 如果值不存在会初始化为0再增加</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> Long <span class="title function_">incrementHashValue</span><span class="params">(String redisKey, String hashKey, Long delta)</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> redisTemplate.opsForHash().increment(redisKey, hashKey, delta);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 从hash中获取值</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> redisKey 要获取的redis键</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> hashKey 要获取的hash键</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@return</span> 获取到的值 值不存在返回null</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> Object <span class="title function_">getHash</span><span class="params">(String redisKey, String hashKey)</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> redisTemplate.opsForHash().get(redisKey, hashKey);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 增加redis键中指定hash键的值并设置过期时间</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> redisKey 要保存的redis键</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> hashKey 要保存的hash键</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param</span> hashValue 要保存的hash值</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">setHashWithDateExpire</span><span class="params">(String redisKey, String hashKey, Object hashValue, String expireDate)</span> &#123;</span><br><span class="line">    setHash(redisKey, hashKey, hashValue);</span><br><span class="line">    redisTemplate.expireAt(redisKey, DateUtil.parse(expireDate).toJdkDate());</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

        
        
      
    </div>
    <footer class="article-footer">
      
        <div class="article-tag-wrap">
          

          
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/wei-blog/tags/%E9%80%BB%E8%BE%91/" rel="tag">逻辑</a></li></ul>

          


        </div>
      
      <!--  -->
      <!--  -->
    </footer>
  </div>
</article>



    <nav id="page-nav">

<a class="extend prev" rel="prev" href="/wei-blog/page/3/">上一页</a><a class="page-number" href="/wei-blog/">1</a><a class="page-number" href="/wei-blog/page/2/">2</a><a class="page-number" href="/wei-blog/page/3/">3</a><span class="page-number current">4</span>
</nav>


</section>
        
          <aside id="sidebar">
  
    
  <div class="widget-wrap">
    <h3 class="widget-title"><i class="fa fa-classify"></i> 分类</h3>
    <div class="widget">
      <ul class="category-list"><li class="category-list-item"><a class="category-list-link" href="/wei-blog/categories/Docker/">Docker</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/wei-blog/categories/Java/">Java</a><span class="category-list-count">3</span></li><li class="category-list-item"><a class="category-list-link" href="/wei-blog/categories/RabbitMQ/">RabbitMQ</a><span class="category-list-count">2</span></li><li class="category-list-item"><a class="category-list-link" href="/wei-blog/categories/Redis/">Redis</a><span class="category-list-count">9</span></li><li class="category-list-item"><a class="category-list-link" href="/wei-blog/categories/Spring/">Spring</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/wei-blog/categories/SpringBoot/">SpringBoot</a><span class="category-list-count">13</span><ul class="category-list-child"><li class="category-list-item"><a class="category-list-link" href="/wei-blog/categories/SpringBoot/RabbitMQ/">RabbitMQ</a><span class="category-list-count">1</span></li></ul></li><li class="category-list-item"><a class="category-list-link" href="/wei-blog/categories/SpringSecurity/">SpringSecurity</a><span class="category-list-count">1</span></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title"><i class="fa fa-tag"></i> 标签</h3>
    <div class="widget">
      <ul class="tag-list" itemprop="keywords"><li class="tag-list-item"><a class="tag-list-link" href="/wei-blog/tags/AOP/" rel="tag">AOP</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/wei-blog/tags/JWT/" rel="tag">JWT</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/wei-blog/tags/MyBatis/" rel="tag">MyBatis</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/wei-blog/tags/MyBatisPlus/" rel="tag">MyBatisPlus</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/wei-blog/tags/%E6%8B%A6%E6%88%AA%E5%99%A8/" rel="tag">拦截器</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/wei-blog/tags/%E8%B7%A8%E5%9F%9F/" rel="tag">跨域</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/wei-blog/tags/%E8%BF%90%E8%A1%8C%E9%94%99%E8%AF%AF/" rel="tag">运行错误</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/wei-blog/tags/%E9%80%BB%E8%BE%91/" rel="tag">逻辑</a><span class="tag-list-count">2</span></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title"><i class="fa fa-posts"></i> 最新文章</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/wei-blog/source/%E5%9F%BA%E4%BA%8ERedis%E7%9A%84%E5%88%86%E5%B8%83%E5%BC%8F%E9%94%81/">基于Redis的分布式锁</a>
          </li>
        
          <li>
            <a href="/wei-blog/source/SpringBoot%E5%AE%9E%E7%8E%B0%E6%8E%A5%E5%8F%A3%E9%99%90%E6%B5%81/">SpringBoot实现接口限流</a>
          </li>
        
          <li>
            <a href="/wei-blog/source/%E7%BC%93%E5%AD%98%E7%A9%BF%E9%80%8F%E3%80%81%E7%BC%93%E5%AD%98%E9%9B%AA%E5%B4%A9%E3%80%81%E7%BC%93%E5%AD%98%E5%87%BB%E7%A9%BF/">(no title)</a>
          </li>
        
          <li>
            <a href="/wei-blog/source/SpringBoot%E6%95%B4%E5%90%88RabbitMQ/">SpringBoot整合RabbitMQ</a>
          </li>
        
          <li>
            <a href="/wei-blog/source/Redis%E4%B9%8BZSet/">(no title)</a>
          </li>
        
      </ul>
    </div>
  </div>


  
</aside>
        
      </div>
      <a id="totop" href="#top"></a>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      <!-- <p>
        <a href="/wei-blog/sitemap.xml">网站地图</a>
        <span> | </span><a href="/wei-blog/atom.xml">订阅本站</a>
        <span> | </span><a href="/wei-blog/about/">联系博主</a>
      </p> -->
      
      <p>
        <span>Copyright &copy; 2023 xueweitao.</span>
        <!-- <span>Theme by <a href="https://github.com/chaooo/hexo-theme-BlueLake/" target="_blank">BlueLake.</a></span>
        <span>Powered by <a href="https://hexo.io/" target="_blank">Hexo.</a></span> -->
      </p>
    </div>
  </div>
</footer>

    </div>
  </div>
  
<script src="/wei-blog/js/jquery-3.4.1.min.js"></script>


<script src="/wei-blog/js/search.json.js"></script>




<script src="/wei-blog/js/script.js"></script>












  



</body>
</html>